<?
include('../../../includes/config.inc.php');
require('../../../includes/classes/fpdf.php');
$eID = $_GET[eID];

$sql = "SELECT C.CatName,P.ProgramName,U.UID,U.NameFirst,U.NameLast,U.Email,U.Phone1,U.PositionID
		FROM casa_event_registration ER 
		LEFT JOIN casa_users U ON ER.UID = U.UID
		LEFT JOIN casa_programs P ON U.ProgramID = P.PID	
		LEFT JOIN casa_categories C ON C.CID = U.PositionID		
		LEFT JOIN casa_events E ON E.EID = '$eID' 
		WHERE ER.EID = '$eID' ORDER BY ProgramName,NameLast";
$result = mysql_db_query($dbname,$sql);
$rows = mysql_num_rows($result);

// Put everything into arrays
$i=0;
while ($value = mysql_fetch_assoc($result)) {
	$uIDArray[$i]       = $value[UID];
	$programArray[$i]   = stripslashes($value[ProgramName]);
	$nameFirstArray[$i] = $value[NameFirst];
	$nameLastArray[$i]  = $value[NameLast];
	$emailArray[$i]     = $value[Email];
	$phoneArray[$i]     = $value[Phone1];
	$positionArray[$i]  = $value[CatName];
	$i++;
}

// Get event title
$sql2 = "SELECT EventTitle from casa_events WHERE EID = '$eID'";
$result2 = mysql_db_query($dbname,$sql2);
$row = mysql_fetch_assoc($result2);
$eventTitle = stripslashes($row[EventTitle]);

define('FPDF_FONTPATH','../../../includes/classes/fonts/');

function gif2jpeg($p_fl, $p_new_fl=null, $bgcolor=false) {
	global $returnImage;
	list($wd, $ht, $tp, $at)=getimagesize($p_fl);
	$img_src=imagecreatefromgif($p_fl);
	$img_dst=imagecreatetruecolor($wd,$ht);
	$clr['red']=255;
	$clr['green']=255;
	$clr['blue']=255;
	if(is_array($bgcolor)) $clr=$bgcolor;
	$kek=imagecolorallocate($img_dst,
	              $clr['red'],$clr['green'],$clr['blue']);
	imagefill($img_dst,0,0,$kek);
	imagecopyresampled($img_dst, $img_src, 0, 0,
	              0, 0, $wd, $ht, $wd, $ht);
	$draw=false;
	if(strlen($p_new_fl)>0){
		if($hnd=fopen($p_new_fl,'w')){
		  $draw=false;
		  fclose($hnd);
		}
	}
	if(true==$draw){
		header("Content-type: image/jpeg");
		imagejpeg($img_dst);
	} else {
		imagejpeg($img_dst, $p_new_fl,100);
		imagedestroy($img_dst);
		imagedestroy($img_src);
	}
	$returnImage = $p_new_fl;
	return $returnImage;	
}

class PDF extends FPDF {
	//Colored table
	function FancyTable($header,$data) {
		//Colors, line width and bold font
		$this->SetFillColor(255,0,0);
		$this->SetTextColor(255);
		$this->SetDrawColor(128,0,0);
		$this->SetLineWidth(.3);
		$this->SetFont('','B');
		//Header
		$w=array(40,35,40,45);
		for($i=0;$i<count($header);$i++)
			$this->Cell($w[$i],7,$header[$i],1,0,'C',1);
		$this->Ln();
		//Color and font restoration
		$this->SetFillColor(224,235,255);
		$this->SetTextColor(0);
		$this->SetFont('');
		//Data
		$fill=0;
		foreach($data as $row) {
			$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
			$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
			$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
			$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
			$this->Ln();
			$fill=!$fill;
		}
		$this->Cell(array_sum($w),0,'','T');
	}
	function make_line($thick=null,$r=0,$g=0,$b=0) {
		if ($thick == null) { $thick = 1; }
		// Set color
		$this->SetFillColor($r,$g,$b);
		$this->Cell(180,$thick,'',0,1,'',1);
	}
	function Footer() {
    	//Go to 1.5 cm from bottom
    	$this->SetY(-15);
    	//Select Arial italic 8
    	$this->SetFont('Arial','I',8);
    	//Print current and total page numbers
    	$this->Cell(0,10,'Page '.$this->PageNo().' of {nb}',0,0,'C');
    }
}

$pdf=new PDF('P','mm','A4');
$pdf->Open();
// Set Top, Right, Left margins at 1"
$pdf->SetMargins(20,23,20);

// For every 8 records, create a page
$tagsTotal = count($uIDArray);
$pagesTotal = ceil($tagsTotal/8);

$i=0;
// Print page(s)
for($a=1;$a<=$pagesTotal;$a++) {
	$pdf->AddPage();
	$imageTop = 25;
	$y = 55;	
	// Print 4 rows
	for($b=0;$b<=3;$b++) {

		// Print 2 cells
		$fullName1 = $nameFirstArray[$i].' '.$nameLastArray[$i];
		$fullName2 = $nameFirstArray[$i+1].' '.$nameLastArray[$i+1];
		$program1  = $programArray[$i];
		$program2  = $programArray[$i+1];
		$i=$i+2;
		
		$logoImage = 'casaPrintLogo.jpg';
		
		// Dimentions are in mm
		// Logo cells
		//$pdf->Cell(89,20.5,'','TLR',0,'L'); 
		//$pdf->Cell(89,20.5,'','TR',1,'L'); 	
		$pdf->Image($logoImage,40,$imageTop,55,20);
		$pdf->Image($logoImage,129,$imageTop,55,20);
		$pdf->Ln(); 
		$pdf->SetY($y);
		$y = $y+60;
				
		// Line 1 cells
		$pdf->SetFont('Helvetica','B',18);
		$pdf->SetTextColor(0);
		$pdf->Cell(89,5,$fullName1,0,0,'C'); 
		$pdf->Cell(89,5,$fullName2,0,1,'C'); 
			
		// Line 2 cells
		$pdf->SetFont('Helvetica','BI',14);
		$pdf->SetTextColor(100);
		$pdf->Cell(89,35,$program1,0,0,'C'); 
		$pdf->Cell(89,35,$program2,0,1,'C'); 	
		$imageTop = $imageTop+60;
	}	
}
$pdf->AliasNbPages(); 
$outputFile = $eventTitle.'_Name_Tags.pdf';
//$output_dir = $pre."user_files/".$userid."_".$acct_no."/pdf/invoices/".$output_file;

$pdf->Output($outputFile,'D');
?>